package com.example.administrator.liveapp.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.BuildConfig;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.utils.DataCleanManager;

/**
 * 账号管理activity
 */

public class AccountManagementPage extends AppCompatActivity implements View.OnClickListener{

    private Button log_out_button;
    private TextView ClearCache;
    private TextView WithRegard;
    private TextView account_management_phone;
    private RelativeLayout weibo,QQ,weixin;
    private TextView weixin_binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_management_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        setData();
    }

    private void setData() {
        setDataCleanManager();
        WithRegard.setText(BuildConfig.VERSION_NAME + "");
        if(ApplicationUtils.getInstance().loginBean!=null){
            if(ApplicationUtils.getInstance().loginBean.getConsumertel()!=null){
                account_management_phone.setText(ApplicationUtils.getInstance().loginBean.getConsumertel().toString());
                if(ApplicationUtils.getInstance().loginBean.getWxopenid()!=null){
                    weixin_binding.setText("已绑定");
                }
            }else if(ApplicationUtils.getInstance().loginBean.getWxopenid()!=null){
                account_management_phone.setText("绑定手机号");
                weibo.setVisibility(View.GONE);
                QQ.setVisibility(View.GONE);
                weixin.setVisibility(View.GONE);
            }
        }
        if(ApplicationUtils.getInstance().loginBean==null){
            log_out_button.setText("立即登录");
        }else{
            log_out_button.setText("退出登录");
        }
    }

    /**
     * ----清除缓存
     */
    private void setDataCleanManager() {
        try {
            ClearCache.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        weibo = (RelativeLayout) findViewById(R.id.weibo);
        QQ = (RelativeLayout) findViewById(R.id.QQ);
        weixin = (RelativeLayout) findViewById(R.id.weixin);
        log_out_button = (Button) findViewById(R.id.log_out_button);
        ClearCache = (TextView) findViewById(R.id.clear_cache);
        WithRegard = (TextView) findViewById(R.id.with_regard);
        weixin_binding = (TextView) findViewById(R.id.weixin_binding);
        account_management_phone = (TextView) findViewById(R.id.account_management_phone);
        findViewById(R.id.account_management_returns).setOnClickListener(this);
        account_management_phone.setOnClickListener(this);
        findViewById(R.id.clearCache).setOnClickListener(this);
        log_out_button.setOnClickListener(this);
        ClearCache.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.clearCache:
                /**
                 * ----弹窗确认是否清除缓存
                 */
                android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
                builder.setMessage("是否清除缓存");
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DataCleanManager.clearAllCache(getApplicationContext());
                        setDataCleanManager();
                    }
                });
                builder.show();
                break;
            case R.id.log_out_button:
                SharedPreferences sp = getSharedPreferences("Setting", 0);
                sp.edit().clear().commit();
                ApplicationUtils.getInstance().loginBean = null;
                Intent intent = new Intent();
                intent.setClass(AccountManagementPage.this,WxLoginPage.class);
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                finish();
                break;
            case R.id.account_management_returns:
                finish();
                break;
            case R.id.account_management_phone:
                if(account_management_phone.getText().equals("绑定手机号")){
                    Intent intent1 = new Intent(AccountManagementPage.this,BindingPhonePage.class);
                    startActivity(intent1);
                    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                    finish();
                }
                break;
        }
    }
}
